package com.reactim.message.config.validation;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
import jakarta.validation.ValidatorFactory;
import java.util.Set;

/**
 * 简单的验证测试运行器
 * 用于验证自定义验证注解的功能
 */
public class ValidationTestRunner {
    
    public static void main(String[] args) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();
        
        System.out.println("开始测试配置验证功能...");
        
        // 测试连接超时验证
        testConnectionTimeoutValidation(validator);
        
        // 测试批次大小验证
        testBatchSizesValidation(validator);
        
        // 测试Redis键前缀验证
        testRedisKeyPrefixValidation(validator);
        
        System.out.println("配置验证功能测试完成！");
    }
    
    private static void testConnectionTimeoutValidation(Validator validator) {
        System.out.println("\n=== 测试连接超时验证 ===");
        
        // 测试有效值
        TestConnectionConfig validConfig = new TestConnectionConfig();
        validConfig.setTimeout(30);
        Set<ConstraintViolation<TestConnectionConfig>> violations = validator.validate(validConfig);
        System.out.println("有效连接超时时间(30分钟) - 验证结果: " + (violations.isEmpty() ? "通过" : "失败"));
        
        // 测试无效值 - 过小
        TestConnectionConfig invalidConfig1 = new TestConnectionConfig();
        invalidConfig1.setTimeout(0);
        violations = validator.validate(invalidConfig1);
        System.out.println("无效连接超时时间(0分钟) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
        
        // 测试无效值 - 过大
        TestConnectionConfig invalidConfig2 = new TestConnectionConfig();
        invalidConfig2.setTimeout(2000);
        violations = validator.validate(invalidConfig2);
        System.out.println("无效连接超时时间(2000分钟) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
    }
    
    private static void testBatchSizesValidation(Validator validator) {
        System.out.println("\n=== 测试批次大小验证 ===");
        
        // 测试有效值
        TestBatchConfig validConfig = new TestBatchConfig();
        validConfig.setBatchSizes(new int[]{1000, 5000, 10000});
        Set<ConstraintViolation<TestBatchConfig>> violations = validator.validate(validConfig);
        System.out.println("有效批次大小数组 - 验证结果: " + (violations.isEmpty() ? "通过" : "失败"));
        
        // 测试无效值 - 包含0
        TestBatchConfig invalidConfig1 = new TestBatchConfig();
        invalidConfig1.setBatchSizes(new int[]{0, 1000, 5000});
        violations = validator.validate(invalidConfig1);
        System.out.println("无效批次大小数组(包含0) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
        
        // 测试无效值 - 空数组
        TestBatchConfig invalidConfig2 = new TestBatchConfig();
        invalidConfig2.setBatchSizes(new int[]{});
        violations = validator.validate(invalidConfig2);
        System.out.println("无效批次大小数组(空数组) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
    }
    
    private static void testRedisKeyPrefixValidation(Validator validator) {
        System.out.println("\n=== 测试Redis键前缀验证 ===");
        
        // 测试有效值
        TestRedisConfig validConfig = new TestRedisConfig();
        validConfig.setKeyPrefix("reactim:connection:");
        Set<ConstraintViolation<TestRedisConfig>> violations = validator.validate(validConfig);
        System.out.println("有效Redis键前缀 - 验证结果: " + (violations.isEmpty() ? "通过" : "失败"));
        
        // 测试无效值 - 不以冒号结尾
        TestRedisConfig invalidConfig1 = new TestRedisConfig();
        invalidConfig1.setKeyPrefix("reactim:connection");
        violations = validator.validate(invalidConfig1);
        System.out.println("无效Redis键前缀(不以冒号结尾) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
        
        // 测试无效值 - 包含空格
        TestRedisConfig invalidConfig2 = new TestRedisConfig();
        invalidConfig2.setKeyPrefix("reactim connection:");
        violations = validator.validate(invalidConfig2);
        System.out.println("无效Redis键前缀(包含空格) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
        
        // 测试无效值 - 过短
        TestRedisConfig invalidConfig3 = new TestRedisConfig();
        invalidConfig3.setKeyPrefix("a:");
        violations = validator.validate(invalidConfig3);
        System.out.println("无效Redis键前缀(过短) - 验证结果: " + (violations.isEmpty() ? "失败" : "通过"));
        if (!violations.isEmpty()) {
            System.out.println("  错误信息: " + violations.iterator().next().getMessage());
        }
    }
    
    // 测试用的配置类
    static class TestConnectionConfig {
        @ValidConnectionTimeout
        private Integer timeout;
        
        public Integer getTimeout() {
            return timeout;
        }
        
        public void setTimeout(Integer timeout) {
            this.timeout = timeout;
        }
    }
    
    static class TestBatchConfig {
        @ValidBatchSizes
        private int[] batchSizes;
        
        public int[] getBatchSizes() {
            return batchSizes;
        }
        
        public void setBatchSizes(int[] batchSizes) {
            this.batchSizes = batchSizes;
        }
    }
    
    static class TestRedisConfig {
        @ValidRedisKeyPrefix
        private String keyPrefix;
        
        public String getKeyPrefix() {
            return keyPrefix;
        }
        
        public void setKeyPrefix(String keyPrefix) {
            this.keyPrefix = keyPrefix;
        }
    }
}